import router from "./router";
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import pinia from './store'
import userStore from "./store/modules/user";
import setting from "./setting/setting";
nprogress.configure({ showSpinner: false })
const useUserStore = userStore(pinia)
// 全局前置守卫示例  
router.beforeEach(async (to, from, next) => {
    nprogress.start()   //加载进度条
    /* const {token} = useUserStore */
    const token = useUserStore.token
    const username = useUserStore.username
    if (token) {
        //登陆成功，不能访问login
        if (to.path == '/login') {
            next({ path: '/' })
        } else {
            if (username) {
                next();
            } else {
                try {
                    await useUserStore.getUserInfo()
                    next({ ...to })
                } catch (error) {
                    //token过期
                    //用户手动修改token
                    await useUserStore.exitUserInfo()
                    next({ path: '/login', query: { redirect: to.path } })
                }
            }
        }
    } else {
        if (to.path == '/login') {
            next();
        } else {
            next({ path: '/login' })
        }
    }
});

// 全局后置守卫示例  
router.afterEach((to, from, next) => {
    nprogress.done()  //加载进度条结束
    document.title = setting.title + '-' + to.meta.title     //网页title
});